﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YYTmEditer.MtObject;
using yyycommon;
using yyylib;

namespace YYTmEditer
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class TmEditerWindow : Window, IYYRunable
    {
        public TmEditerWindow()
        {
            InitializeComponent();
        }
        private GameObject gameobj;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (ProgramUtil.ProcExist("MonsterTrain"))
            {
                MessageBoxResult result = MessageBox.Show("先退出游戏再运行修改器,是否退出?", "请先退出游戏", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    ProgramUtil.CloseProc("MonsterTrain");
                }
            }
            gameobj = new GameObject();
            GameObject.Instance = gameobj;
            try
            {
                FileVersionInfo fv = FileVersionInfo.GetVersionInfo("YYTmEditer.dll");
                string vs = fv.FileMajorPart + "." + fv.FileMinorPart + "." + fv.FileBuildPart + fv.FilePrivatePart;
                this.lable_info.Content = "版本:"+vs;
                this.Title = this.Title + "-" + vs;
            }
            catch (Exception ecp)
            {
                this.lable_info.Content = ecp.Message;
            }
            NameMap.loadConfig();
            load();
            this.Activated += this.WindowActive;
            this.tabControl.SelectionChanged += this.tabControl_SelectionChanged;
        }


        private void WindowActive(object sender, EventArgs e) {
            load();
        }

        private void load()
        {
            if (gameobj.load())
            {
                MessageBoxResult result = MessageBox.Show("您的游戏已经产生新的存档,如果不重新加载,将会使用当前存档,是否重新加载?", "发现新的存档", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    gameobj.load(true);
                }
            }
            this.RefreshInfo();
        }

        private void RefreshInfo()
        {
            game_info.Content = gameobj.GetStartCondition().Info()+" "+gameobj.GetLevel();
            textGold.Text = "" + gameobj.Gold;
            textHp.Text = "" + gameobj.Hp;
            textHpMax.Text = "" + gameobj.HpMax;
        }


        private void button_Click(object sender, RoutedEventArgs e)
        {
            gameobj.Gold = int.Parse(textGold.Text);
            gameobj.Hp = int.Parse(textHp.Text);
            gameobj.HpMax = int.Parse(textHpMax.Text);
            if (gameobj.Save())
            {
                MessageBox.Show("保存成功,请打开游戏");
            }
        }

        public void Open()
        {
            this.ShowDialog();

        }

        public void ExitOnClose(bool v)
        {
            if (v)
            {
                this.Closed += this.Exit;
            }
        }

        private void Exit(object sender,EventArgs e) {
            if (Application.Current != null)
            {
                Application.Current.Shutdown();
            }
        }

        private void TabItem_GotFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private BlessingControll blessingControll;
        private UnitControll unitControll;
        private CardLibController cardsControll;
        private string lastSelectTabName;
        private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabControl tc = sender as TabControl;
            TabItem ct = (TabItem)tc.SelectedItem;
            if (null!=ct) {
                if (ct.Name != lastSelectTabName) {
                    lastSelectTabName = ct.Name;                
                    Console.WriteLine(ct.Name + " selected");
                    if ("info".Equals(ct.Name))
                    {
                        this.RefreshInfo();
                    }
                    else if ("blessing".Equals(ct.Name))
                    {
                        if (blessingControll == null)
                        {
                            blessingControll = new BlessingControll();
                            var gd = ct.Content as Grid;
                            gd.Children.Add(blessingControll);
                        }
                    }
                    else if ("card".Equals(ct.Name))
                    {
                        if (unitControll == null)
                        {
                            unitControll = new UnitControll();
                            var gd = ct.Content as Grid;
                            gd.Children.Add(unitControll);
                        }
                    }
                    else if ("cardlib".Equals(ct.Name)) {
                        if (cardsControll == null)
                        {
                            cardsControll = new CardLibController();
                            var gd = ct.Content as Grid;
                            gd.Children.Add(cardsControll);
                        }
                    }
                }

            }
        }

        private void openurl_Click(object sender, RoutedEventArgs e)
        {
            Process.Start("https://www.baidu.com/link?url=-43MvdM_RoRQ0XG1s6RYXw9-AQNDXQysr9G2A5dxMA1X-g0nlWAszqWuD8krW6P8&wd=&eqid=8867d09c00031913000000025ef1adef");
        }
    }
}
